#include "GameObject.h"


GameObject::GameObject(LPDIRECT3DDEVICE9 device):_device(device)
{
	Radius = 3.0f;
	x=0;
	y=0;
	z=0;
}

GameObject::GameObject(LPDIRECT3DDEVICE9 device, float X, float Y, float Z, float YROT):_device(device)
{
	x = X;
	y = Y;
	z = Z;
	Yrot = D3DXToRadian(YROT);
	Radius = 25.0f;
}

GameObject::~GameObject(void)
{
	if (XAnimator_ != nullptr)
	{
		XAnimator_ = nullptr;
	}
}

void GameObject::loadModel(LPDIRECT3DDEVICE9 device_)
{
	Model.LoadMesh(device_, "bones_all.x");
}

void GameObject::loadModelXA(IXAnimator *XAnim, LPDIRECT3DDEVICE9 device_, std::string modelFile)
{
	D3DXVECTOR3 minBoundsModelSpace,maxBoundsModelSpace;
	XAnimator_ = XAnim;
	Model.LoadMeshXA(XAnimator_, device_, modelFile.c_str());
	TMeshBounds *indMeshBoundsModelSpace=XAnimator_->GetBoundingShapes(Model.meshIdNumber_,&minBoundsModelSpace,&maxBoundsModelSpace,NULL,NULL,NULL);
	ObjectBoundingBoxCorners[0] = D3DXVECTOR3( minBoundsModelSpace.x, minBoundsModelSpace.y, minBoundsModelSpace.z ); // xyz
	ObjectBoundingBoxCorners[1] = D3DXVECTOR3( maxBoundsModelSpace.x, minBoundsModelSpace.y, minBoundsModelSpace.z ); // Xyz
	ObjectBoundingBoxCorners[2] = D3DXVECTOR3( minBoundsModelSpace.x, maxBoundsModelSpace.y, minBoundsModelSpace.z ); // xYz
	ObjectBoundingBoxCorners[3] = D3DXVECTOR3( maxBoundsModelSpace.x, maxBoundsModelSpace.y, minBoundsModelSpace.z ); // XYz
	ObjectBoundingBoxCorners[4] = D3DXVECTOR3( minBoundsModelSpace.x, minBoundsModelSpace.y, maxBoundsModelSpace.z ); // xyZ
	ObjectBoundingBoxCorners[5] = D3DXVECTOR3( maxBoundsModelSpace.x, minBoundsModelSpace.y, maxBoundsModelSpace.z ); // XyZ
	ObjectBoundingBoxCorners[6] = D3DXVECTOR3( minBoundsModelSpace.x, maxBoundsModelSpace.y, maxBoundsModelSpace.z ); // xYZ
	ObjectBoundingBoxCorners[7] = D3DXVECTOR3( maxBoundsModelSpace.x, maxBoundsModelSpace.y, maxBoundsModelSpace.z ); // XYZ
}

void GameObject::Render()
{
	Model.Render(x,y,z,Yrot);
}

void GameObject::RenderXA()
{
	Model.RenderXA(x,y,z,Yrot);
	UpdateBoundingBoxes();

	//D3DCOLOR colour(D3DCOLOR_XRGB(255,255,255));
	//D3DXVECTOR3 front[4],back[4];
	//front[0] = BoundingBox[0];
	//front[1] = BoundingBox[2];
	//front[2] = BoundingBox[3];
	//front[3] = BoundingBox[1];

	//back[3] = BoundingBox[4];
	//back[2] = BoundingBox[6];
	//back[1] = BoundingBox[7];
	//back[0] = BoundingBox[5];
	//DBG_GFX.RenderAABBox(AABoundingBox[0], AABoundingBox[1], D3DCOLOR_XRGB(0,0,255));
	//DBG_GFX.RenderOBBox(front,back,colour);
}

void GameObject::UpdateBoundingBoxes()
{
	D3DXMATRIX TranslateMat, TranslateMat2, ScaleMat, ObjMat, RotYMat;
	D3DXMatrixIdentity(&TranslateMat);								// Set WorldMat to identity matrix
	D3DXMatrixIdentity(&ScaleMat);
	D3DXMatrixIdentity(&RotYMat);
	D3DXMatrixIdentity(&ObjMat);

	D3DXMatrixScaling(&ScaleMat,0.5,1,1);
	D3DXMatrixRotationY(&RotYMat, Yrot);
	D3DXMatrixTranslation(&TranslateMat, x, y, z);
	D3DXMatrixMultiply(&ObjMat,&ScaleMat,&RotYMat);
	D3DXMatrixMultiply(&ObjMat,&ObjMat,&TranslateMat);

	for( int i = 0; i < 8; i++ )
		D3DXVec3TransformCoord( &BoundingBox[i], &ObjectBoundingBoxCorners[i], &ObjMat );

	D3DXVECTOR3 minAA, maxAA;
	minAA.x = maxAA.x = BoundingBox[0].x;
	minAA.y = maxAA.y = BoundingBox[0].y;
	minAA.z = maxAA.z = BoundingBox[0].z;
	for (int i=1;i<8;i++)
	{
		if (BoundingBox[i].x < minAA.x) 
			minAA.x= BoundingBox[i].x;
		if (BoundingBox[i].x > maxAA.x) 
			maxAA.x= BoundingBox[i].x;

		if (BoundingBox[i].y < minAA.y) 
			minAA.y= BoundingBox[i].y;
		if (BoundingBox[i].y > maxAA.y) 
			maxAA.y= BoundingBox[i].y;

		if (BoundingBox[i].z < minAA.z) 
			minAA.z= BoundingBox[i].z;
		if (BoundingBox[i].z > maxAA.z) 
			maxAA.z= BoundingBox[i].z;
		AABoundingBox[0] = minAA;
		AABoundingBox[1] = maxAA;
	}
}